﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class RegDeta : System.Web.UI.Page
{
    string Quantit;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
            int Quan = Convert.ToInt32(Quantit);
            if (Quan == 0)
            {
                TxtVisTime.Enabled = false;
                BtnGh.Enabled = false;
            }
            else
            {
                TxtVisTime.Enabled = true;
                BtnGh.Enabled = true;
            }
        }
    }

    private void Bind()
    {
        string Dor = Request.QueryString["Dorid"];
        string Date= Request.QueryString["Date"];
        string Time= Request.QueryString["Time"];
        string Dauy = "";
        string sql = string.Format("select * from Scheduling where DocID='{0}' and DauyDate='{1}' and StartTime='{2}'", Dor,Date,Time);
        SqlDataReader dr = DBHelper.GetDataReader(sql);
        while (dr.Read())
        {
            LblDocName.Text = dr["DocName"].ToString();
            LblDetName.Text = dr["DetName"].ToString();
            LblStartTime.Text = dr["StartTime"].ToString();
            LblEndTime.Text = dr["EndTime"].ToString();
            LblPosition.Text = dr["Position"].ToString();
            LblRoom.Text = dr["ConsultationRoom"].ToString();
            LblQuantity.Text = dr["Quantity"].ToString();
            Quantit= dr["Quantity"].ToString();
            Dauy = dr["DauyDate"].ToString();
        }
        dr.Close();
        int p = Dauy.Length;
        Dauy = Dauy.Substring(0, p - 7);
        LblWeek.Text = Dauy;
        Image1.ImageUrl = string.Format("~/image/Photo/{0}.png", Dor);
    }

    protected void BtnGh_Click(object sender, EventArgs e)
    {
        string Current = DateTime.Now.ToShortDateString().ToString();
        string Name = Session["Name"].ToString();
        string sql = string.Format("select count(*) from Register where PatIDCard='{0}' and RegTime BETWEEN '{1}' and dateadd(day, 1, '{1}')", Name, Current);
        int n = Convert.ToInt32(DBHelper.GetScalar(sql));
        if (n >= 3)
        {
            Response.Write("<script> alert('挂号失败，每天只能在线上挂3个号，如需挂号请前往窗口办理！')</script>");
        }
        else
        {
            if (TxtVisTime.Text == "")
            {
                Response.Write("<script> alert('挂号失败！')</script>");
            }
            else
            {
                string Dor = Request.QueryString["Dorid"];
                string IDCard = Session["Name"].ToString();
                DateTime VisTime = Convert.ToDateTime(TxtVisTime.Text);
                string insertsql = string.Format("insert into Register (DocID,DocName,DepName,PatIDCard,VisTime,DauyDate) values('{0}','{1}','{2}','{3}','{4}','{5}')", Dor, LblDocName.Text, LblDetName.Text, IDCard, VisTime, LblWeek.Text);
                DBHelper.GetNonQuery(insertsql);
                string update = string.Format("update Scheduling set Quantity = Quantity-1 where DocID='{0}' and DauyDate='{1}' and StartTime='{2}'", Dor, LblWeek.Text, LblStartTime.Text);
                DBHelper.GetNonQuery(update);
                Response.Write("<script> alert('挂号成功!');window.location.href='Registration.aspx'; </script>");
            }
        }
    }

    protected void VisTime_TextChanged(object sender, EventArgs e)
    {
        DateTime VisTime = Convert.ToDateTime(TxtVisTime.Text);
        DateTime EndTIme = Convert.ToDateTime(LblEndTime.Text);
        DateTime StrTIme = Convert.ToDateTime(LblStartTime.Text);
        if (VisTime > EndTIme || VisTime < StrTIme)
        {
            Response.Write("<script> alert('就诊时间有误，请重新选择！')</script>");
            TxtVisTime.Text = "";
        }
    }
}